#include "button.h"
#include "device.h"
#include <string.h>

DEV_REG_DEF(button, Button_t)

DEV_RET_t
button_monitor(Device_t* pstDevice){

    // 输入参数检查
    if(NULL == pstDevice || NULL == pstDevice->pvFeature) return DEV_PARAM_ERR;

    // 设备检测
    CHECK_DEVICE(pstDevice);

    // 转换为Button_t结构体
    Button_t *pstBtn = GET_DEVICE(pstDevice, Button_t);

    // 输入参数检查
    if(NULL == pstBtn->HW.pfnRead) return DEV_PARAM_ERR;

    // 读取按键状态
    uint8_t u8NowState = pstBtn->HW.pfnRead();

    // 判断按键状态是否发生变化
    if(pstBtn->SW.u8State != u8NowState){

        // 更新状态
        pstBtn->SW.u8State = u8NowState;

        // 判断按键状态
        if(pstBtn->SW.u8State == pstBtn->SW.u8Active) set_device_event(pstDevice, BUTTON_CLICKED);
        else set_device_event(pstDevice, BUTTON_RELEASED);
    }

    // BUTTON执行成功
    DEV_RETURN(DEV_OK);
}
